import { createRouter, createWebHistory } from 'vue-router'

// import { createRouter, createWebHashHistory } from "vue-router";


const routers = [

  {
    path: '/',
    name: '/',
    // component: () => import('@/App.vue')
    component: () => import('@/views/base.vue'),
    children:[
      {
        path: '/home',
        name: 'home',
        // component: () => import('@/App.vue')
        component: () => import('@/views/home.vue')
      },
      {
        path: '/homeSet',
        name: 'homeSet',
        // component: () => import('@/App.vue')
        component: () => import('@/views/Page/homeSet.vue')
      },
      {
        path: '/animalSet',
        name: 'animalSet',
        // component: () => import('@/App.vue')
        component: () => import('@/views/Page/animalSet.vue')
      },
      {
        path: '/articlesSet',
        name: 'articlesSet',
        // component: () => import('@/App.vue')
        component: () => import('@/views/Page/articlesSet.vue')
      },
      {
        path: '/userSet',
        name: 'userSet',
        // component: () => import('@/App.vue')
        component: () => import('@/views/Page/userSet.vue')
      },
      {
        path: '/TieziSet',
        name: 'TieziSet',
        // component: () => import('@/App.vue')
        component: () => import('@/views/Page/TieziSet.vue')
      },
      {
        path: '/archivesSet',
        name: 'archivesSet',
        component: () => import('@/views/Page/archivesSet.vue')
      },
      {
        path: '/password',
        name: 'password',
        // component: () => import('@/App.vue')
        component: () => import('@/views/user/password.vue')
      },
      {
        path: '/profile',
        name: 'profile',
        // component: () => import('@/App.vue')
        component: () => import('@/views/user/profile.vue')
      },
    ]
  },
  
  {
    path: '/login',
    name: 'login',
    // component: () => import('@/App.vue')
    component: () => import('@/views/login/loginP.vue')
  },
 
  // {
  //   path: '/loginOut',
  //   name: 'LoginOut',
  //   component: () => import('@/views/user/loginOut.vue')
  // },

]
const router = createRouter({
  history: createWebHistory(),
  
  // 哈希模式 
  // history:createWebHashHistory(),
  routes: routers
})

function getLogin(){
  if (!localStorage.getItem('token')){
    return false
  }
  else{
    return true
  }
}

router.beforeEach((to, from, next) => {
  // 如果目标路径不是登录页 并且 用户未登录，则重定向到登录页
  if (to.path !== '/login' && !getLogin()) {
    next('/login');
    // window.location.href = '/login';
  } else {
    next(); // 否则继续导航
  }
});


export default router
